package br.edu.fucapi.remo.control.mb;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import br.edu.fucapi.remo.model.bean.Associado;
import br.edu.fucapi.remo.model.dao.JPAUtil;

@FacesConverter(forClass=Associado.class)
public class AssociadoConverter implements Converter {
 
	@Override
	public Object getAsObject(FacesContext context, UIComponent component, String string) {
		System.out.println("AssociadoConverter.getAsObject(): " + string);
		if(string == null || string.isEmpty()){
			return null;
		}
		return JPAUtil.getEntityManager().find(Associado.class, Integer.valueOf(string));
	}
 
	@Override
	public String getAsString(FacesContext context, UIComponent component, Object object) {
		Associado associado = (Associado) object;
		System.out.println("AssociadoConverter.getAsString(): " + associado);
		if(associado == null || associado.getId() == null){
			return null;
		}
		return String.valueOf(associado.getId());
	}
}